package com.atguigu.commonutil;

import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.Map;

/**
 * @author Aug28-tlsjn
 * @date 2020-06-29 Monday 15:45
 */
@Data
@AllArgsConstructor
@Accessors(chain = true)
public class ApiResponse<T> implements Serializable {

    @ApiModelProperty(value = "操作是否成功")
    private Boolean success;

    @ApiModelProperty(value = "状态码，200成功，400失败")
    private Integer code;

    @ApiModelProperty(value = "状态信息")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private T data;

    private ApiResponse() {}

    /**
     * 快捷地返回一个成功的结果封装
     * @return 成功结果的封装
     */
    public static ApiResponse<Map<String, Object>> successInstance() {
        return new ApiResponse<>(true, ResultCode.SUCCESS, "操作成功~(*^_^*)", null);
    }

    /**
     * 快捷地返回一个失败的结果封装
     * @return 失败结果的封装
     */
    public static ApiResponse<String> failedInstance() {
        return new ApiResponse<>(false, ResultCode.FAILED, "操作失败！/(ㄒoㄒ)/~~", "");
    }
}
